﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BankClient {

    public partial class RegistrationForm : Form {

        private Form parent;
        private BankServiceProxy service;

        public RegistrationForm(Form parent, BankServiceProxy service) {
            InitializeComponent();

            this.parent = parent;
            this.service = service;

            // Events
            this.btnRegister.Click += new EventHandler(this.FormButtons_Click);
            this.btnCancel.Click += new EventHandler(this.FormButtons_Click);
            this.FormClosing += new FormClosingEventHandler(this.RegistrationForm_FormClosing);
        }

        void FormButtons_Click(object sender, EventArgs e) {
            Button b = sender as Button;
            if (b.Equals(this.btnCancel))
                this.Close();
            else if (b.Equals(this.btnRegister)) {
                if (this.txtPassword1.Text == this.txtPassword2.Text) {
                    bool result = this.service.Register(this.txtName.Text,
                        this.txtAddress.Text,
                        this.txtPassword1.Text);
                    if (result) {
                        MessageBox.Show("Registration complete.");
                        this.Close();                                                
                    } else
                        MessageBox.Show("An error occured while registering.");
                } else
                    MessageBox.Show("The two passwords do not match.");
            }
        }

        void RegistrationForm_FormClosing(object sender, FormClosingEventArgs e) {
            this.parent.Show();
        }
    }
}
